<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    >
      <template #btns>
        <el-button
          v-if="meta.indexOf('OrderListExport') > -1"
          :loading="subLoading"
          type="primary"
          @click="handleExport"
        >导出</el-button>
      </template>
    </QueryForm>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-tableb"
    >
      <el-table-column
        align="center"
        label="订单编号"
        min-width="220"
        prop="id"
        show-overflow-tooltip
      >
        <template v-slot="scope">
          <span class="primaryColor pointer" @click="handleDetail(scope.row)">{{
            scope.row.id
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="会员信息"
        min-width="220"
        prop="memberInfo"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="产品名称"
        min-width="120"
        prop="productName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="价格(元)"
        min-width="100"
        prop="price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="实付(元)"
        min-width="100"
        prop="realPrice"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="支付方式"
        min-width="120"
        prop="payMethodName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="支付时间"
        min-width="160"
        prop="payTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="退款金额"
        min-width="100"
        prop="refundMoney"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="退款时间"
        min-width="160"
        prop="refundTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="下单时间"
        min-width="160"
        prop="createTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="备注"
        min-width="120"
        prop="note"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="订单状态"
        min-width="120"
        prop="payStatusName"
        fixed="right"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        min-width="130"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="
                scope.row.payStatus == 0 && meta.indexOf('CancelOrder') > -1
              "
              v-preventReClick
              type="text"
              @click="onceAgain(scope.row.id, '0')"
            >取消订单</el-button>
            <el-button
              v-if="scope.row.payStatus == 3 && meta.indexOf('Refund') > -1"
              v-preventReClick
              type="text"
              @click="onceAgain(scope.row.id, '1')"
            >退款</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Detail ref="DetailRef" />
  </div>
</template>

<script>
import { queryFormConfig } from './orderRecord'
import { momMOrderList, momMOrderListEx, momCancelOrder, momRefund } from '@/api'
import dayjs from 'dayjs'
import Detail from './orderDetail.vue'
import QueryForm from '@/components/QueryForm'
export default {
  components: {
    Detail,
    QueryForm
  },
  data() {
    return {
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      meta: [],
      total: 0,
      listLoading: false,
      queryFormConfig,
      subLoading: false,

      filters: {
        selDate: []
      }
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    let time = []
    const start = new Date()
    const end = new Date()
    start.setTime(end.getTime() - 3600 * 1000 * 24 * 29)
    time = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
    this.$set(this.filters, 'selDate', time)
    this.getList()
  },
  methods: {
    handleExport() {
      this.subLoading = true
      const { filters } = this
      const param = { ...filters }
      if (param.selDate.length > 0) {
        param.startTime = param.selDate[0] + ' 00:00:00'
        param.endTime = param.selDate[1] + ' 23:59:59'
      }
      momMOrderListEx({ param })
        .then((res) => {
          this.subLoading = false
          if (res.errorCode === '000000') {
            window.open(res.record.showUrl, '_blank')
          }
        })
        .catch(() => {
          this.subLoading = false
        })
    },
    onceAgain(id, type) {
      const message = type === '1' ? '确定退订选中的订单吗？退订后会员有效期会扣除，相应款项将原路退回' : '确定取消选中的订单吗？取消后订单立即失效'
      const title = type === '1' ? '退款' : '取消订单'
      const fn = type === '1' ? momRefund : momCancelOrder
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        fn({ param: { id }}).then(res => {
          this.$message.success(`${title}成功`)
          this.getList()
        })
      })
    },
    handleDetail(item) {
      this.$refs.DetailRef.getOrderDetail(item.id)
      this.$refs.DetailRef.isShow = true
    },
    getList(page) {
      this.listLoading = true
      const { pagination, filters } = this
      const param = { ...filters }
      if (page) {
        this.pagination.page = page
      }
      if (param.selDate.length > 0) {
        param.startTime = param.selDate[0] + ' 00:00:00'
        param.endTime = param.selDate[1] + ' 23:59:59'
      }
      momMOrderList({ pagination, param })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.total = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList(1)
    },
    clearList() {
      this.filters = {
        selDate: []
      }
      this.getList(1)
    }
  }
}
</script>

<style>
</style>
